package com.start_eternity.oasissafewalk.utils;

// 计算两个经纬度之间的距离
public class CalculateDistance {

    private static final double EQUATOR_RADIUS = 6378.137; //赤道半径

    // 计算两个经纬度之间的距离
    public static double calculateDistance(double StationLatitude, double StationLongitude, double OrderLatitude, double OrderLongitude) {
        //纬度
        double lat1 = Math.toRadians(StationLatitude);
        double lat2 = Math.toRadians(OrderLatitude);
        // 经度
        double lon1 = Math.toRadians(StationLongitude);
        double lon2 = Math.toRadians(OrderLongitude);
        // 纬度之差
        double a = lat1 - lat2;
        // 经度之差
        double b = lon1 - lon2;
        // 计算两点距离的公式
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        // 弧长乘赤道半径, 返回单位: 米
        s = s * EQUATOR_RADIUS;
        return s;
    }
}
